Imports System
Imports System.Configuration
Imports System.Xml
Imports System.Web
Imports System.Windows.Forms
Imports System.Globalization

''' <summary>
''' Methods to read from and write to the application config file.
''' </summary>
Public Class ConfigUtil

#Region "Private constructor"

	Private Sub New()
		' Can't create instance!
	End Sub

#End Region

#Region "Get"

	''' <summary>
	''' Gets a connection string settings object from the config file.
	''' </summary>
	''' <returns>ConnectionStringSettings object.</returns>
	Public Shared Function GetConnectionString(ByVal key As String) As ConnectionStringSettings

		Dim objConnectionString As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(key)

		If Not objConnectionString Is Nothing AndAlso objConnectionString.ConnectionString.Length > 0 AndAlso objConnectionString.ProviderName.Length > 0 Then
			Return objConnectionString
		Else
			Return Nothing
		End If

	End Function

	''' <summary>
	''' Gets a custom application config value as String, defaults to Empty.
	''' </summary>
	''' <returns>Key value as string, if null returns Empty.</returns>
	Public Shared Function GetConfig(ByVal key As String) As String

		Dim strConfig As String = ConfigurationManager.AppSettings(key)

		If Not strConfig Is Nothing AndAlso strConfig.Length > 0 Then
			Return strConfig
		Else
			Return String.Empty
		End If

	End Function

	''' <summary>
	''' Gets an application config from appSettings as String.
	''' </summary>
	''' <returns>If null returns the default, otherwise the value as String.</returns>
	Public Shared Function GetConfig(ByVal key As String, ByVal strDefault As String) As String

		Dim strConfig As String = ConfigurationManager.AppSettings(key)

		If Not strConfig Is Nothing AndAlso strConfig.Length > 0 Then
			Return strConfig
		Else
			Return strDefault
		End If

	End Function

	''' <summary>
	''' Gets an application config from appSettings as Int32.
	''' </summary>
	''' <returns>If null returns the default, otherwise the value as Int32.</returns>
	Public Shared Function GetConfig(ByVal strKey As String, ByVal intDefault As Integer) As Integer

		Dim strConfig As String = ConfigurationManager.AppSettings(strKey)

		If Not strConfig Is Nothing AndAlso strConfig.Length > 0 Then
			Return Convert.ToInt32(strConfig, CultureInfo.InvariantCulture)
		Else
			Return intDefault
		End If

	End Function

	''' <summary>
	''' Gets an application config from appSettings as Decimal.
	''' </summary>
	''' <returns>If null returns the default, otherwise the value as Decimal.</returns>
	Public Shared Function GetConfig(ByVal strKey As String, ByVal decDefault As Decimal) As Decimal

		Dim strConfig As String = ConfigurationManager.AppSettings(strKey)

		If Not strConfig Is Nothing AndAlso strConfig.Length > 0 Then
			Return Convert.ToDecimal(strConfig, CultureInfo.InvariantCulture)
		Else
			Return decDefault
		End If

	End Function

	''' <summary>
	''' Gets an application config from appSettings as Boolean.
	''' </summary>
	''' <returns>If null returns the default, otherwise the value as Boolean.</returns>
	Public Shared Function GetConfig(ByVal strKey As String, ByVal blnDefault As Boolean) As Boolean

		Dim strConfig As String = ConfigurationManager.AppSettings(strKey)

		If Not strConfig Is Nothing AndAlso strConfig.Length > 0 Then
			Return DataUtil.TrueOrFalse(strConfig)
		Else
			Return blnDefault
		End If

	End Function

	''' <summary>
	''' Gets an application config from appSettings as DateTime.
	''' </summary>
	''' <returns>If null returns the default, otherwise the value as DateTime.</returns>
	Public Shared Function GetConfig(ByVal strKey As String, ByVal dtmDefault As DateTime) As DateTime

		Dim strConfig As String = ConfigurationManager.AppSettings(strKey)

		If Not strConfig Is Nothing AndAlso strConfig.Length > 0 Then
			Return DateTime.Parse(strConfig)
		Else
			Return dtmDefault
		End If

	End Function

#End Region

#Region "Write"

	''' <summary>
	''' Writes a Boolean value to the configuration settings.
	''' </summary>
	Public Shared Sub WriteConfig(ByVal strKey As String, ByVal blnValue As Boolean)

		WriteConfig(strKey, blnValue.ToString())

	End Sub

	''' <summary>
	''' Writes an Int32 value to the configuration settings.
	''' </summary>
	Public Shared Sub WriteConfig(ByVal strKey As String, ByVal intValue As Integer)

		WriteConfig(strKey, intValue.ToString())

	End Sub

	''' <summary>
	''' Writes a String value to the configuration settings.
	''' </summary>
	Public Shared Sub WriteConfig(ByVal strKey As String, ByVal strValue As String)

		Dim xmlDoc As New XmlDocument
		Dim xmlElement As XmlNode
		Dim xmlAttribute As XmlAttribute
		Dim strPath As String

		If AssemblyUtil.GetAppType() = AppTypeEnum.AspNet Then
			strPath = HttpContext.Current.Request.PhysicalApplicationPath & "web.config"
		Else
			strPath = Application.ExecutablePath & ".config"
		End If

		Try

			xmlDoc.Load(strPath)
			xmlElement = xmlDoc.SelectSingleNode("/configuration/appSettings/add[@key='" & strKey & "']")

			If xmlElement Is Nothing Then
				xmlElement = xmlDoc.CreateElement("add")
				xmlAttribute = xmlDoc.CreateAttribute("key")
				xmlAttribute.Value = strKey
				xmlElement.Attributes.Append(xmlAttribute)
				xmlAttribute = xmlDoc.CreateAttribute("value")
				xmlAttribute.Value = strValue
				xmlElement.Attributes.Append(xmlAttribute)
				xmlDoc.SelectSingleNode("/configuration/appSettings").AppendChild(xmlElement)
			Else
				xmlElement.Attributes("value").Value = strValue
			End If

			xmlDoc.Save(strPath)

		Catch ex As Exception
			Throw
		End Try

	End Sub

#End Region

End Class